package h1202;

import java.sql.*;

public class Curd {
    public static final String url = "jdbc:mysql://localhost:3306/kgc81?useSSLE=false$serverTimezone=Asia/Shanghai";
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
//        int add = crud("insert into s_grade (gname) values ('java81')");
//        if (add==0){
//            System.out.println("添加失败");
//        }else if (add!=0){
//            System.out.println("添加成功");
//        }
//        int del = crud(" delete from s_grade where id=1");
//        System.out.println(del);
//        int upd = crud("update s_grade set gname='jdbc' where id=29");
//        System.out.println(upd);
          crud("select * from s_grade");
    }


    public static int crud (String sql) {
        try {
            Connection con = DriverManager.getConnection(url, "root", "1234");
            Statement st = con.createStatement();
            if (sql.startsWith("select")) {
                ResultSet rs = st.executeQuery(sql);
                while (rs.next()) {
                    long id = rs.getLong("id");
                    String sbname = rs.getString("gname");
                    System.out.println("编号：" + id + " 年级名称：" + sbname);
                }
                rs.close();
                st.close();
                con.close();
                return 1;
            } else {
                int rs = st.executeUpdate(sql);
                st.close();
                con.close();
                return rs;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }
    }
}
